// miniprogram/pages/main/home/index.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    catchtouchmove: true,
    value1: "ai",
    option1: [{
        text: '综合排序',
        value: "ai"
      },
      {
        text: '起送价最低',
        value: "delivery"
      },
      {
        text: '活动最优惠',
        value: "discount"
      },
    ],
    noMore: false,
    slogan:""

  },
  touchDropdown() {
    this.setData({
      catchtouchmove: false
    })
  },
  touchDropup() {
    this.setData({
      catchtouchmove: true
    })
  },
  change(value){
    console.info(value.detail);
    this.sortType=value.detail;

    this.init();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //首页幻灯片
    var params = {};
    params.key = "home";
    params.needLogin = false;
    //app.postRequest("/rest/ad/list.htm", params,this.backAdList);

    var nav = {};
    nav.key = "nav";
    nav.needLogin = false;
    //app.postRequest("/rest/ad/list.htm", nav,this.backNavList);
    this.init();

  },
  init() {
    this.no = 1;
    var params = {};
    params.no = this.no;
    params.size = 10;
    params.auditState="checked";
    if(this.sortType){
      params.sortType=this.sortType;
    }
    if(app.location){
      params.lat=app.location.lat;
      params.lng=app.location.lng;
    }
    app.dataRequest("/rest/shop/search.htm", params, this, "shops");

    var site={};
    site.id=app.tenant;
    app.postRequest("/rest/sitetenant/view.htm", site, this.backTenantData);

    
    
  },
  backTenantData(res){
    if (res.code == 0) {
      var slogan="";
      slogan= res.demo;
      this.setData({
        slogan: slogan
      });
    }
  },
  backAdList(res) {
    if (res.code == 0) {
      that.setData({
        banners: res.list
      });
    }
  },
  backNavList(res) {
    if (res.code == 0) {
      that.setData({
        navs: res.list
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  goSearch: function () {
    wx.navigateTo({
      url: '/pages/main/search/index',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  search: function (event) {
    var type = event.target.dataset.type;
    console.info(type);
    this.sortType=type;
    this.init();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.info(this.totalPage);
    if (this.no < this.totalPage) {
      this.no = this.no + 1;
      this.setData({
        noMore: false
      });
      var params = {};
      params.no = this.no;
      params.size = 10;
      params.auditState="checked";
      if(this.sortType){
        params.sortType=this.sortType;
      }
      if(app.location){
        params.lat=app.location.lat;
        params.lng=app.location.lng;
      }
      app.loadMore("/rest/shop/search.htm", params, this, "shops");

    } else {
      this.setData({
        noMore: true
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})